package Kolos2;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;


public class Java3 implements ActionListener{

	JFrame window = new JFrame("Okno");
	JPanel panel = new JPanel();
	
	JTableHeader tabelkaHead;
	
//	DefaultTableModel model = new DefaultTableModel();
	JTable tabelka = new JTable();
	DefaultTableModel model;
	
	JLabel jplp = new JLabel("Lp:");
	JLabel jpimie = new JLabel("Imie:");
	JLabel jpnaziwsko = new JLabel("Nazwisko:");
	
	JTextField lp = new JTextField();
	JTextField imie = new JTextField();
	JTextField nazwisko = new JTextField();
	
	JButton add = new JButton("Dodaj");
	JButton save = new JButton("Zapisz");
	
	ArrayList<String[]> t = new ArrayList<String[]>();
	String[][] tab;
	
	public Java3() {
		
		model = (DefaultTableModel) tabelka.getModel();
		
		model.addColumn("Lp");
		model.addColumn("Imie");
		model.addColumn("Nazwisko");
		model.addRow(new String[] {"1","Krystian","Kaniowski"});
		model.addRow(new String[] {"2","Krystian","Kaniowski"});		
		
		tabelkaHead = tabelka.getTableHeader();
		
		add.setActionCommand("add");
		add.addActionListener(this);
		
		save.setActionCommand("save");
		save.addActionListener(this);
		
		panel.add(jplp);
		panel.add(lp);
		panel.add(jpimie);
		panel.add(imie);
		panel.add(jpnaziwsko);
		panel.add(nazwisko);
		panel.add(add);
		panel.add(save);
		
		panel.setLayout( new GridLayout( 4, 2) );
		
		window.add(tabelkaHead, BorderLayout.NORTH);
		window.add(tabelka, BorderLayout.CENTER);
		window.add(panel, BorderLayout.SOUTH);
		
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.pack();
		window.setVisible(true);
		
	}
	
	public String[][] buildTable(ArrayList a){
		String[][] tab = new String[a.size()][3]; 
		int j = 0;
		Iterator<String[]> i = a.iterator();
		while( i.hasNext()){
			tab[j] = i.next();
			j++;
		}
		return tab;
	}
	
	public static void main(String[] args) {
		new Java3();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		switch (e.getActionCommand()){
		case "add":
			model.addRow(new String[]{lp.getText(), imie.getText(), nazwisko.getText()});
			window.pack();
			break;
		case "save":
			JFileChooser fc = new JFileChooser();
			try{
				if (fc.showSaveDialog(null)==JFileChooser.APPROVE_OPTION){
					BufferedWriter br = new BufferedWriter(new FileWriter(fc.getSelectedFile()));
					for(int i=0; i< model.getColumnCount(); i++){
						br.write(model.getColumnName(i) + "|");
					}
					
					br.write("\r\n");
					
					for(int i=0; i< model.getRowCount(); i++){
						for(int j=0; j< model.getColumnCount(); j++){
							br.write(model.getValueAt(i, j) + "|");
						}
						br.write("\r\n");
					}
					
					br.close();
				}
			} catch (IOException e1) {
				
			}
			break;
		}
	}
	
	

}
